package dryven.model.binding.fieldset;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class FieldSet {

	private Map<String,FieldGroup> _groups = new HashMap<String, FieldGroup>();
	private FieldGroup _defaultGroup;
	
	public FieldSet() {
		
	}
	
	public FieldGroup getDefaultGroup() {
		if(_defaultGroup==null) {
			_defaultGroup = group("default");
		}
		return _defaultGroup;
	}
	
	public FieldGroup group(String nameKey) {
		FieldGroup group = _groups.get(nameKey);
		if(group==null) {
			group = new FieldGroup(nameKey, this);
			_groups.put(nameKey,group);
		}
		return group;
	}
	
	public FieldSet add(FieldSet fs) {
		return this;
	}
	
	public Iterable<FieldGroup> groups() {
		return _groups.values();
	}
	
	public Iterable<Class<?>> getModelTypes() {
		Collection<Class<?>> modelTypes = new LinkedList<Class<?>>();
		for (FieldGroup group : this.groups()) {
			for (Field field : group.getFields()) {
				if(!modelTypes.contains(field.getModelType())) {
					modelTypes.add(field.getModelType());
				}
			}
		}
		return modelTypes;
	}
}
